#include "CircleMovable.h"
#define THETA 0.0001
CircleMovable::CircleMovable(Vector2D & centre,double rayon,GameRectangle & arene):GameObject(arene),_centre(centre),_rayon(rayon)
{
}
void CircleMovable::Paint(wxDC &dc)
{
	GameObject::Paint(dc);
	dc.DrawCircle(_centre.X,_centre.Y,_rayon);
}
void CircleMovable::SetDirection(Vector2D & direct)
{ 
	_direction = direct.normalize();
}
CircleMovable::~CircleMovable()
{
}
void CircleMovable::CalculerEnglobant()
{
	if(_englobant!=NULL)
		delete _englobant;
	_invalidEnglobant = false;
	_englobant = new GameRectangle(_centre,_rayon);
}

int CircleMovable::Collision(DrawableObject *obj)
{
	CircleMovable *objet = dynamic_cast<CircleMovable*>(obj);
	if(objet == NULL)
		return GameObject::Collision(obj);
	return CollisionCercle(_centre,_rayon,objet->_centre,objet->_rayon);
}
int CircleMovable::CollisionCercle(Vector2D & c1,double r1, Vector2D &c2, double r2,double marge)
{
	double x = c2.X - c1.X,
		   y = c2.Y - c1.Y,
		   rayon = r2 + r1;
	return (x * x + y * y) < (rayon*rayon+marge);
}
void CircleMovable::CalculerDirection(CircleMovable *obj)
{
	double x = fabs(obj->_centre.X-_centre.X);
	double y = fabs(obj->_centre.Y-_centre.Y);
	if(x < THETA)
		_direction.X*= -1;

	if(y < THETA)
		_direction.Y*= -1;

	_direction.Y*=-1;
	_direction.X*=-1;

}
void CircleMovable::CalculerDirection(int i)
{
	switch(i)
	{
	case 1:
	case 2:
		_direction.X*=-1;
		break;
	case 3:
	case 4:
		_direction.Y*=-1;
		break;
	case 5:
	case 6:
	case 7:
	case 8:
		_direction.Y*=-1;
		_direction.X*=-1;
		break;
	}
}
void CircleMovable::Move()
{
	_invalidEnglobant = true;
	_centre+=_direction;
	GameRectangle englobant = Englobant();
	if(englobant.BL.X<_arene.BL.X)
		_centre.X = _arene.BL.X+_rayon;
	if(englobant.BL.Y>_arene.BL.Y)
		_centre.Y = _arene.BL.Y-_rayon;
	if(englobant.TR.X>_arene.TR.X)
		_centre.X = _arene.TR.X-_rayon;
	if(englobant.TR.Y<_arene.TR.Y)
		_centre.Y = _arene.TR.Y+_rayon;
	_invalidEnglobant = true;
}